package org.example;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TreeSet;

public class Main {


    public static void main(String[] args) {
        // 读取 文件路径
        String filePath = "/Users/fanfan/Desktop/zxbank.xlsx";
        // 写入 文件路径
        File targetFile = new File("/Users/fanfan/Desktop/人员账务.txt");
        // 通过treeSet保证对象有序
        TreeSet<PeopleEntity> treeSet = new TreeSet<>();
        try {
            EasyExcel.read(filePath, ReadEntity.class, new PageReadListener<ReadEntity>(dataList -> {
                for (ReadEntity item : dataList) {
                    // 存款 xx元
                    String deposit = item.getDeposit().substring(0, item.getDeposit().length() - 1);
                    PeopleEntity build = PeopleEntity.builder().seq(item.getSeq()).name(item.getName()).age(item.getAge()).sex(item.getSex()).city(item.getCity())
                            // 最终格式的存款
                            .finalDeposit(deposit)
                            // 存款时间去掉元
                            .deposit(new BigDecimal(deposit))
                            // 存款时间
                            .depositTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date(item.getDepositTime())))
                            // 计算第几天
                            .currentDay(MonthToDayEnums.calDay(new Date(item.getDepositTime()))).build();
                    treeSet.add(build);
                }
            })).sheet().doRead();
            // 构造文件写入字符流
            FileWriter fileWriter = new FileWriter(targetFile);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            boolean flag = false;
            for (PeopleEntity peopleEntity : treeSet) {
                if (flag) {
                    bufferedWriter.write(peopleEntity.toString());
                    flag = true;
                } else {
                    bufferedWriter.newLine();
                    bufferedWriter.write(peopleEntity.toString());
                }
            }
            // 关闭流
            bufferedWriter.close();
            fileWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }

    }
}